//扩展jQuery对json字符串的转换
jQuery.extend({
	/**
	 * *
	 * @see 将json字符串转换为对象 *
	 * @param json字符串 *
	 * @return 返回object,array,string等对象
	 */
	evalJSON : function(strJson) {
        try{
            return eval("("+strJson+")");
        }catch(e){
            j.alert("转换json时出错:"+e+","+strJson,true);
        }
        return {};
	},
    /**
     * 判断一个对象是否为数组
     * @param m
     * @return {*}
     */
    isArray : function(m){
        /*return m&&console.log(
            typeof m==='object'? //是否'object'
                typeof m.length==='number'&& //数组的length必为'number'
                    typeof m.slice==='function'&& //数组必然拥有slice()方法
                    !(m.propertyIsEnumerable('length'))? //数组的length不可枚举(for in)
                    m.length?
                        m.length===1?m[0]:m.join(','):
                        'an empty array':
                    m.construcctor:
                typeof m
        );*/
        if(m===null){
            return false;
        }
        return (typeof m === "object" && typeof m.length === "number");
    },
    /**
     * 计算一个整形的随机数
     * @return {Number}
     */
    randomInt:function(){
        return Math.ceil(Math.random()*1000000);
    }
});
jQuery.extend({
    /**
     * 将form序列化成json格式的对象
     * @param form
     */
    serializeJson : function(form){
        var json = {};
        if(!form){
            form = this;
        }
        var cs = $("[name]",form);
        var c = null;
        var name,val;
        cs.each(function(i,item){
            c = $(item);
            name = c.attr("name");
            if(typeof json[name] != "undefined"){
                json[name]+=c.val()+",";
            }else{
                json[name] = c.val();
            }
        });
        return json;
    },
	/**
	 * *
	 * 
	 * @see 将javascript数据类型转换为json字符串 *
	 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp *
	 * @return 返回json字符串
	 */
	toJSON : function(object) {
		var type = typeof object;
		if ("object" == type) {
            if(object == null)//必须在此处考虑是否为null，否则将会出错
                type = "object";
			if (Array == object.constructor)
				type = "array";
			else if (RegExp == object.constructor)
				type = "regexp";
			else
				type = "object";
		}
		switch (type) {
		case "undefined":
		case "unknown":
            return "";
			break;
		case "function":
		case "boolean":
		case "regexp":
			return object.toString();
			break;
		case "number":
			return isFinite(object) ? object.toString() : "null";
			break;
		case "string":
			return "'"
					+ object.replace(/(\\|\')/g, '\\$1')
							.replace(
								/\n|\r|\t/g,
								function() {
									var a = arguments[0];
									return (a == "\n") ? "\\n"
											: (a == "\r") ? "\\r"
													: (a == "\t") ? "\\t" : '';
								}) + "'";
			break;
		case "object":
			if (object === null)
				return "null";
			var results = [];
			for ( var property in object) {
				var value = jQuery.toJSON(object[property]);
				if (value !== undefined)
					results.push(jQuery.toJSON(property) + ":" + value);
			}
			return "{" + results.join(",") + "}";
			break;
		case "array":
			var results = [];
			for ( var i = 0; i < object.length; i++) {
				var value = jQuery.toJSON(object[i]);
				if (value !== undefined)
					results.push(value);
			}
			return "[" + results.join(",") + "]";
			break;
		}
	}
});

/**
 * 简单的对话框
 */
(function($){
    $.fn.openD = function(src,title,loadingText) {
        if(typeof title === "string" && title.length>0){
            $(this).find(".titleBar span").html(title);
        }
        var $this = $(this);
        $this.css("display","block").find(".details").html(loadingText);
        $this.position({
            at:"center center",
            of: "body"
        });
        Http.doGetHtml(src,null,function(html){
            $this.find(".details").html(html);
        });
    };
    $.fn.closeD = function(){
        var $this = $(this);
        $this.css("display","none");
    }
})(jQuery);